package com.maxatime.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maxatime.dao.StudentBasicInfoMapper;
import com.maxatime.dao.StudentRegisterMapper;
import com.maxatime.pojo.dto.StudentBasicInfoDTO;
import com.maxatime.pojo.entity.StudentBasicInfo;
import com.maxatime.pojo.entity.StudentRegister;
import com.maxatime.service.StudentBasicInfoService;
import com.maxatime.util.PasswordManagerUtil;
import com.maxatime.util.SerialNumberUtil;

@Service
public class StudentBasicInfoService1 implements StudentBasicInfoService{
	
	@Autowired
	StudentBasicInfoMapper StudentBasicInfoMapper;
	@Autowired
	StudentRegisterMapper studentRegisterMapper;
	
	@Override
	public int deleteByPrimaryKey(StudentBasicInfoDTO record) {
		StudentBasicInfo StudentBasicInfo=new StudentBasicInfo();
		BeanUtils.copyProperties(record, StudentBasicInfo);
		StudentBasicInfoMapper.deleteByPrimaryKey(StudentBasicInfo);
		StudentRegister StudentRegister=new StudentRegister();
		StudentRegister.setStudentId(record.getStudentId());
		studentRegisterMapper.deleteByPrimaryKey(StudentRegister);
		return 0;
	}

	@Override
	public int insert(StudentBasicInfo record) {
		
		return 0;
	}

	@Override
	@Transactional
	public int insertSelective(StudentBasicInfoDTO record) {
		String studentNum="ABC"+"时间戳"+"随机数";
		StudentBasicInfo studentBasicInfo=new StudentBasicInfo();
		String studentId=SerialNumberUtil.getUUID();
		BeanUtils.copyProperties(record,studentBasicInfo);
		studentBasicInfo.setStudentId(studentId);
//		讲string转化为地址值
//		Integer.toHexString(System.identityHashCode(studentName))
//		随机数
//		String studentId=SerialNumberUtil.getUUID();
//		或者
//		studentBasicInfo.setStudentId(Integer.toHexString(SerialNumberUtil.getUUID());
		Date date=new Date();
		studentBasicInfo.setCreateTime(date);
		studentBasicInfo.setUpdateTime(date);
		studentBasicInfo.setDeleteFlag("0");
		StudentBasicInfoMapper.insertSelective(studentBasicInfo);
		PasswordManagerUtil passwordManagerUtil=new PasswordManagerUtil();
		String a="123456";
		String password=passwordManagerUtil.getMd5DigestAsHex(a);
		
		StudentRegister studentRegister=new StudentRegister();
		BeanUtils.copyProperties(record,studentRegister);
		studentRegister.setStudentId(studentId);
		studentRegister.setPassword(password);
		studentRegister.setStatus("0");
		studentRegister.setDeleteFlag("0");
		studentRegister.setCreateTime(date);
		studentRegister.setUpdateTime(date);
		studentRegisterMapper.insertSelective(studentRegister);
		return 0;
	}

	@Override
	public Map<String, Object> selectByPrimaryKey(StudentBasicInfo studentBasicInfo) {
		
		
		//分页
		PageHelper.startPage(studentBasicInfo.getPageNum(),studentBasicInfo.getPageSize());
		
		List<StudentBasicInfo> list=StudentBasicInfoMapper.selectByPrimaryKey(studentBasicInfo);
		
		PageInfo<StudentBasicInfo> pageInfo= new PageInfo<StudentBasicInfo>(list);
		Map<String, Object> m =new HashMap<>();
	    m.put("list", list);
	    m.put("pageInfo", pageInfo);
		return m;
	}
	public List<StudentBasicInfo> selectByPrimaryKey1() {
		List<StudentBasicInfo> list=StudentBasicInfoMapper.selectByPrimaryKey1();
		return list;
	}
	@Override
	@Transactional
	public int updateByPrimaryKeySelective(StudentBasicInfoDTO record) {
		String studentNum="ABC"+"时间戳"+"随机数";
		StudentBasicInfo studentBasicInfo=new StudentBasicInfo();
		BeanUtils.copyProperties(record,studentBasicInfo);
		
//		讲string转化为地址值
//		Integer.toHexString(System.identityHashCode(studentName))
//		随机数
//		String studentId=SerialNumberUtil.getUUID();
//		或者
//		studentBasicInfo.setStudentId(Integer.toHexString(SerialNumberUtil.getUUID());
		Date date=new Date();
		
		studentBasicInfo.setUpdateTime(date);
		studentBasicInfo.setDeleteFlag("0");
		StudentBasicInfoMapper.updateByPrimaryKeySelective(studentBasicInfo);
		PasswordManagerUtil passwordManagerUtil=new PasswordManagerUtil();
		String a="123456";
		String password=passwordManagerUtil.getMd5DigestAsHex(a);
		
		StudentRegister studentRegister=new StudentRegister();
		BeanUtils.copyProperties(record,studentRegister);
		studentRegister.setPassword(password);
		studentRegister.setStatus("0");
		studentRegister.setDeleteFlag("0");
		
		studentRegister.setUpdateTime(date);
		studentRegisterMapper.updateByPrimaryKeySelective(studentRegister);
		return 0;
	}
	@Override
	public int updateByPrimaryKey(StudentBasicInfo record) {
		// TODO Auto-generated method stub
		return 0;
	}

	//反显
	@Override
	public List<StudentBasicInfo> finddata(String studentId) {
		// TODO Auto-generated method stub
		
		return StudentBasicInfoMapper.finddata(studentId);
	}
	
	@Override
	public List<StudentBasicInfo> selectByPrimaryKey3(StudentBasicInfo studentBasicInfo) {
		// TODO Auto-generated method stub
		return StudentBasicInfoMapper.selectByPrimaryKey3(studentBasicInfo);
	}
   
}